﻿using ProductMonitor.Model;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using static System.Net.Mime.MediaTypeNames;

namespace ProductMonitor.UserControls
{
    /// <summary>
    /// RaderUC.xaml 的交互逻辑
    /// </summary>
    public partial class RaderUC : UserControl
    {
        public RaderUC()
        {
            InitializeComponent();
            SizeChanged += OnSizeChange;
        }

        /// <summary>
        /// 依赖属性，雷达图数据源
        /// </summary>
        public List<RaderModel> ItemSource 
        {
            get { return (List<RaderModel>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty,value); }
        }

        public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSource",typeof(List<RaderModel>),typeof(RaderUC)) ;

        /// <summary>
        /// 窗口变化后改变大小
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void OnSizeChange(object obj,SizeChangedEventArgs e) 
        {
            Drag();
        }

        public void Drag() 
        {
            //判断是否有数据
            if (ItemSource != null&&ItemSource.Count>0) 
            {
                mainCanvas.Children.Clear();
                P1.Points.Clear();
                P2.Points.Clear();
                P3.Points.Clear();
                P4.Points.Clear();
                P5.Points.Clear();


                //调整雷达图大小
                double size = Math.Min(RenderSize.Width,RenderSize.Height);
                LayoutGrid.Width = size;
                LayoutGrid.Height = size;

                //半径
                double raduis = size / 2;
                //每一步步子跨度
                double step = 360.0 / ItemSource.Count;

                for(var i =0;i< ItemSource.Count;i++) 
                {
                    //错误的
                    //double x = raduis + (raduis - 20 * Math.Cos(step * i - 90) * Math.PI / 180);//x偏移量
                    //double y = raduis + (raduis - 20 * Math.Sin(step * i - 90) * Math.PI / 180);//y偏移量

                    double x = (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180);//x偏移量
                    double y =  (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180);//y偏移量
                                                                                          //X,Y坐标

                    P1.Points.Add(new Point(raduis + x, raduis + y));
                    P2.Points.Add(new Point(raduis + x * 0.75, raduis + y * 0.75));
                    P3.Points.Add(new Point(raduis + x * 0.5, raduis + y * 0.5));
                    P4.Points.Add(new Point(raduis + x * 0.25, raduis + y * 0.25));

                    //数据多边形
                    P5.Points.Add(new Point(raduis+x * ItemSource[i].Value*0.01, raduis + y * ItemSource[i].Value * 0.01));

                    //文字处理
                    TextBlock textBlock = new();
                    textBlock.Width=60;
                    textBlock.FontSize = 10;
                    textBlock.TextAlignment = TextAlignment.Center;
                    textBlock.Text = ItemSource[i].ItemName;
                    textBlock.Foreground = new SolidColorBrush(Color.FromArgb(100,255,255,255));
                    textBlock.SetValue(Canvas.LeftProperty, raduis + (raduis - 10) * Math.Cos((step * i - 90) * Math.PI / 180) - 30);//设置左边间距
                    textBlock.SetValue(Canvas.TopProperty, raduis + (raduis - 10) * Math.Sin((step * i - 90) * Math.PI / 180) - 7);//设置上边间距

                    //  textBlock.SetValue(Canvas.LeftProperty,rea);
                    mainCanvas.Children.Add(textBlock);
                    if (i == ItemSource.Count - 1) 
                    {
                        var a = 1;
                    }
                }
            }
        }
    }
}
